﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using UDS.Entity;

namespace UDS.Components
{
    /// <summary>
    /// 部门处理类
    /// </summary>
    public class Position
    {
        #region Revised

        /// <summary>
        /// Get the information about the given position.
        /// </summary>
        /// <param name="id">The id of the position.</param>
        public static PositionInfo GetPositionInfo(int id)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@Position_id", SqlDbType.Int, 4, id)
            };
            using (IDataReader dr = db.RunDataReader("dbo.sproc_GetPositionInfo_V2", parameters))
            {
                if (dr.Read())
                {
                    return PositionInfo.ReadPositionFromIDataReader(dr);
                }
            }
            return null;
        }

        public static IList<PositionInfo> GetAllPositions()
        {
            List<PositionInfo> list = new List<PositionInfo>();
            var db = new Database();
            using (IDataReader dr = db.RunDataReader("dbo.sproc_GetAllPosition", null))
            {
                while (dr.Read())
                {
                    var p = PositionInfo.ReadPositionFromIDataReader(dr);
                    list.Add(p);
                }
            }
            return list;
        }

        /// <summary>
        /// Get all child positions of the given one (excluding).
        /// </summary>
        public static IList<PositionInfo> GetSubPositions(int parentId)
        {
            if (parentId < 0)
            {
                throw new ArgumentOutOfRangeException("parentId");
            }

            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@Position_id", SqlDbType.Int, 4, parentId),
            };

            List<PositionInfo> list = new List<PositionInfo>();
            using (IDataReader dr = db.RunDataReader("dbo.sproc_GetAllChildPosition_V2", parameters))
            {
                while (dr.Read())
                {
                    var p = PositionInfo.ReadPositionFromIDataReader(dr);
                    list.Add(p);
                }
            }
            return list;
        }

        #endregion Revised

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="ParentPositionID">本部门的父部门id</param>
        /// <param name="name">本部门的名称</param>
        /// <param name="description">本部门的描述</param>
        /// <returns>本部门分配到的id</returns>
        public static long Add(long ParentPositionID, string name, string description)
        {
            var db = new Database();
            var parameters = new SqlParameter[3];

            parameters[0] = new SqlParameter();
            parameters[1] = new SqlParameter();
            parameters[2] = new SqlParameter();


            parameters[0].SqlDbType = SqlDbType.Int;
            parameters[0].ParameterName = "@Position_ParentID";
            parameters[0].Value = ParentPositionID;


            parameters[1].SqlDbType = SqlDbType.NVarChar;
            parameters[1].Size = 255;
            parameters[1].ParameterName = "@Position_Name";
            parameters[1].Value = name.Trim();


            parameters[2].SqlDbType = SqlDbType.Text;
            parameters[2].ParameterName = "@Position_Remark";
            parameters[2].Value = description.Trim();


            //parameters[3] = new SqlParameter();
            //parameters[3].SqlDbType = SqlDbType.Int;
            //parameters[3].ParameterName = "@ID";
            //parameters[3].Direction = ParameterDirection.ReturnValue;

            return (db.RunProc("dbo.sproc_AddPosition", parameters));
        }

        /// <summary>
        /// 添加考勤时间
        /// </summary>
        /// <param name="PositionID">部门id</param>
        /// <param name="ondutytime">上班时间</param>
        /// <param name="offdutytime">下班时间</param>
        /// <returns></returns>
        public static int AddDutyTime(long PositionID, DateTime ondutytime, DateTime offdutytime)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@positionid", SqlDbType.Int, 4, PositionID),
                Database.MakeInParam("@ondutytime", SqlDbType.DateTime, 8, ondutytime),
                Database.MakeInParam("@offdutytime", SqlDbType.DateTime, 8, offdutytime)
            };
            return (db.RunProc("dbo.sproc_WA_AddPositionDutyTime", parameters));
        }

        /// <summary>
        /// 更改部门考勤时间
        /// </summary>
        /// <param name="PositionID">部门id</param>
        /// <param name="ondutytime">上班时间</param>
        /// <param name="offdutytime">下班时间</param>
        /// <returns></returns>
        public static int UpdateDutyTime(long PositionID, DateTime ondutytime, DateTime offdutytime)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@positionid", SqlDbType.Int, 4, PositionID),
                Database.MakeInParam("@ondutytime", SqlDbType.NVarChar, 20, ondutytime.ToShortTimeString()),
                Database.MakeInParam("@offdutytime", SqlDbType.NVarChar, 20, offdutytime.ToShortTimeString())
            };
            return (db.RunProc("dbo.sproc_WA_UpdatePositionDutyTime", parameters));
        }

        public static int DeleteDutyTime(long PositionID)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@positionid", SqlDbType.Int, 4, PositionID)
            };
            return (db.RunProc("dbo.sproc_WA_DeletePositionDutyTime", parameters));
        }


        public static int Delete(long PositionID)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@Position_id", SqlDbType.Int, 4, PositionID)
            };
            return (db.RunProc("dbo.sproc_DeletePosition", parameters));
        }

        public static int Modify(long PositionID, string dep_name, string dep_description)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@position_id", SqlDbType.Int, 4, PositionID),
                Database.MakeInParam("@position_name", SqlDbType.NVarChar, 300, dep_name.Trim()),
                Database.MakeInParam("@position_description", SqlDbType.NVarChar, 300, dep_description.Trim())
            };
            return (db.RunProc("dbo.sproc_UpdatePositionInfo", parameters));
        }

        public SqlDataReader GetPositionDutyTime(long PositionID)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@PositionID", SqlDbType.Int, 4, PositionID)
            };
            return db.RunDataReader("dbo.sproc_GetPositionDutyTime", parameters);
        }

        public int MoveDeparement(long FromPositionID, long ToPositionID)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@FromPositionID", SqlDbType.Int, 4, FromPositionID),
                Database.MakeInParam("@ToPositionID", SqlDbType.Int, 4, ToPositionID),
            };
            return (db.RunProc("dbo.sproc_MovePosition", parameters));
        }

        #region 人员职位级别更新

        /// <summary>
        /// 人员职位级别更新
        /// </summary>
        /// <param name="PositionID">职位ID</param>
        /// <param name="NewCaste">新职级</param>
        /// <returns>返回是否成功</returns>
        public static int UpdateCaste(long PositionID, int NewCaste)
        {
            var db = new Database();

            var parameters = new[]
            {
                Database.MakeInParam("@PositionID", SqlDbType.Int, 4, PositionID),
                Database.MakeInParam("@NewCaste", SqlDbType.Int, 4, NewCaste)
            };
            return db.RunProc("dbo.sproc_UpdatePositionCaste", parameters);
        }

        #endregion
    }
}